import {getTopBanners,getRecommend} from '@/services/recommend'
import {getNewAlbum,getRonking} from '@/services/recommend'
import {CHANGE_TOP_BANNERS} from './constants'
import {CHANGE_HOT_RECOMMED,CHANGE_NEW_ALBUM,CHANGE_NEWRANKING,CHANGE_UPRANKING,CHANGE_ORIGINRANKING} from './constants'

const changeTopBannersAction = (res)=>({
    type:CHANGE_TOP_BANNERS,
    topBanners:res.data.banners
})
//getTopBannersAction 发ajax 获取banner
export const getTopBannersAction = () =>{
    return (dispatch) =>{
        getTopBanners().then(res=>{
            // console.log(res);
            dispatch(changeTopBannersAction(res))
        })
    }
}
 const changeHotRecommend = (res)=>({
    type:CHANGE_HOT_RECOMMED,
    hotRecommend:res.data.result
})
export const getHotRecommend = (limit)=>{
    return (dispatch)=>{
        getRecommend(limit).then(res=>{
            dispatch(changeHotRecommend(res))
        })
    }
}
 const changeNewAlbumData = (res)=>({
    type:CHANGE_NEW_ALBUM,
    newAlbum:res.data.albums
    })

export const getNewAlbumData = (limit)=>{
    return (dispatch)=>{
        getNewAlbum(limit).then(res=>{
            //dispatch中传入的为一个对象 执行的是reducer中提前定义的代码
            dispatch(changeNewAlbumData(res))
        })
    }
}

const changeUpRankingData= (res)=>({
    type:CHANGE_UPRANKING,
    upRanking:res.data.playlist
})
const changeNewRankingData= (res)=>({
    type:CHANGE_NEWRANKING,
    newRanking:res.data.playlist
})
const changeOriginRankingData= (res)=>({
    type:CHANGE_ORIGINRANKING,
    originRanking:res.data.playlist
})
export const getRonkingData = (idx)=>{
    return (dispatch)=>{
        getRonking(idx).then(res=>{
            switch (idx) {
                case 0:
                    dispatch(changeUpRankingData(res))
                    break;
                case 1:
                    dispatch(changeNewRankingData(res))
                    break;
                case 2:
                    dispatch(changeOriginRankingData(res))
                    break;
                default:
                    break;
            }
        })
    }
}


